package com.wlcorp.forcazamis.sys;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wlcorp.forcazamis.sys.util.DataObjUtils;

public abstract class BaseService {
    protected static final Log log = LogFactory.getLog(BaseService.class); 
    
    public BaseService() {
        super();
        
    }


    @SuppressWarnings("unchecked")
	protected static boolean isValid(Object object) {
        if (null == object){
            return false;
        }else if(object instanceof String){
            return (!object.equals("0") && !object.equals("00") && !object.equals("00:00") && !object.equals("null") && !object.equals(""));
        }else if (object instanceof Integer || object instanceof Float){
            Integer objInteger = (Integer)object;
            if (objInteger.intValue() == 0 ){
                return false;
            }
        }else if (object instanceof ArrayList){
            ArrayList objList = (ArrayList)object;
            if (objList.size() > 0){
                return true;
            }
        }else{
            boolean flag = false;
            Class cls = object.getClass();
            Field fieldList[] = cls.getDeclaredFields();
            for (int i = 0; i < fieldList.length; i++) {
                Field fld = fieldList[i];
                String classFieldName = fld.getName();
                Class classFieldType = fld.getType();
                int mod = fld.getModifiers();
                String modifierName = Modifier.toString(mod);
                boolean isStatic = Modifier.isStatic(mod);
                boolean isFinal = Modifier.isFinal(mod);
                if (null != modifierName && modifierName.equals("private") && !isStatic && !isFinal){
                    try{
                        Object beanObjectValue = DataObjUtils.getBeanPropertyValue(object,classFieldName);
                        if (null != beanObjectValue){
                            if (classFieldType.getName().equals("int") || classFieldType.getName().equals("float")){
                                Integer objInteger = (Integer)beanObjectValue;
                                if (objInteger.intValue() == 0 ){
                                    flag = false;
                                }
                            }else{
                                return true;
                            }
                        }else{
                            flag = false;
                        }
                    }catch (Throwable e) {
                    }
                }
            }
            return flag;
        }
        return false;
    }
    /**
     * 设置出错信息
     * @param baseBean  BaseBean子类
     * @param status	出错状态位
     * @param msg		出错信息
     * @throws Exception
     */
    public void setErrorField(BaseBean baseBean,int status, String msg){
    	if(null != baseBean){
    		baseBean.setStatus(status);
    		baseBean.setMsg(msg);
    	}
    	else
    		System.err.println("the baseBean is null!");
    }
}
